#include <QtGui>
#include "PrefWindow.h"

PrefWindow::PrefWindow(QWidget *parent) : QDialog(parent) {
  QSettings settings;
  
	QMenuBar * menubar = new QMenuBar(this);
	QMenu * ToolsMenu = menubar->addMenu("File");
	ToolsMenu->addAction("Close", this, SLOT(close()), QKeySequence(tr("Ctrl+W")));
  
  //setAttribute(Qt::WA_DeleteOnClose, true);
  
  //QRect space = QApplication::desktop()->availableGeometry();
  //resize(space.width()*0.95, space.height()*.5); //styling
  
  QLabel * favLabel = new QLabel("Favourites directory:");
  favEdit = new QLineEdit;
  QHBoxLayout * fav = new QHBoxLayout;
  fav->addWidget(favLabel);
  fav->addWidget(favEdit);
  
  QLabel * verbsLabel = new QLabel("Verbs directory:");
  verbsEdit = new QLineEdit;
  QHBoxLayout * verbs = new QHBoxLayout;
  verbs->addWidget(verbsLabel);
  verbs->addWidget(verbsEdit);
  
  QLabel * shortcutLabel = new QLabel("Set shortcut:");
  meta = new QCheckBox(QKeySequence(Qt::META).toString(QKeySequence::NativeText));
  ctrl = new QCheckBox(QKeySequence(Qt::CTRL).toString(QKeySequence::NativeText));
  alt = new QCheckBox(QKeySequence(Qt::ALT).toString(QKeySequence::NativeText));
  shift = new QCheckBox(QKeySequence(Qt::SHIFT).toString(QKeySequence::NativeText));
  shortcutEdit = new HKCatcher;
  QHBoxLayout * shortcut = new QHBoxLayout;
  shortcut->addWidget(shortcutLabel);
  shortcut->addWidget(meta);
  shortcut->addWidget(ctrl);
  shortcut->addWidget(alt);
  shortcut->addWidget(shift);
  shortcut->addWidget(shortcutEdit);
  
  
  QVBoxLayout * paths = new QVBoxLayout;
  paths->addLayout(fav);
  paths->addLayout(verbs);
  paths->addLayout(shortcut);
  paths->addStretch();
  
  this->setLayout(paths);
  
  favEdit->setText(settings.value("favDir").toString());
  verbsEdit->setText(settings.value("verbsDir").toString());
  
  int mods = settings.value("hotkeyMod").toInt();
  if (mods & Qt::MetaModifier) meta->setCheckState(Qt::Checked);
  if (mods & Qt::ControlModifier) ctrl->setCheckState(Qt::Checked);
  if (mods & Qt::AltModifier) alt->setCheckState(Qt::Checked);
  if (mods & Qt::ShiftModifier) shift->setCheckState(Qt::Checked);
  shortcutEdit->setText(QKeySequence(settings.value("hotkey").toInt()).toString());
}

void PrefWindow::closeEvent(QCloseEvent *) {
  QSettings settings;
  settings.setValue("favDir", favEdit->text());
  settings.setValue("verbsDir", verbsEdit->text());
  
  int hotkeyMod = 0;
  if (ctrl->checkState()) {
    hotkeyMod += Qt::ControlModifier;
  }
  if (meta->checkState()) {
    hotkeyMod += Qt::MetaModifier;
  }
  if (alt->checkState()) {
    hotkeyMod += Qt::AltModifier;
  }
  if (shift->checkState()) {
    hotkeyMod += Qt::ShiftModifier;
  }
  settings.setValue("hotkeyMod", hotkeyMod);
  if (shortcutEdit->key != 0) {
    settings.setValue("hotkey", shortcutEdit->key);
  }
}

HKCatcher::HKCatcher(QWidget * parent) : QLineEdit(parent) {
  key = 0;
}

void HKCatcher::keyPressEvent(QKeyEvent * e) {
  if (!e->modifiers()) {
    key = e->key();
    setText(QKeySequence(e->key()).toString());
  }
}
